const bodyParser = require('koa-bodyparser')
const path = require("path")
const staticCache = require('koa-static-cache')
const render = require('koa-art-template')
const session = require('koa-session-minimal')
const MysqlStore = require('koa-mysql-session')
const config = require('../config/default')

module.exports = app => {
  // session存储配置
  const sessionMysqlConfig= {
    user: config.database.USERNAME,
    password: config.database.PASSWORD,
    database: config.database.DATABASE,
    host: config.database.HOST
  }

  // 配置session中间件
  app.use(session({
    key: 'USER_SID',
    store: new MysqlStore(sessionMysqlConfig)
  }))


  // 配置模板引擎
  render(app, {
    root: path.join(__dirname, '../views'+ config.mode), // 视图目录
    extname: '.html',
    debug: process.env.NODE_ENV !== 'production'
  })

  app.use(staticCache(path.join(__dirname, '../public'+ config.mode), { dynamic: true }, {
    maxAge: 365 * 24 * 60 * 60
  }))

  // post数据参数限制1MB
  app.use(bodyParser({
    formLimit: '1mb'
  }))
}
